<?php 
 
	header('Content-Type: text/html; charset=utf-8');

	//load core files
	if(!isset($_GET['site'])){
		$_GET['site']='';
	}
	
	require_once('includes/config.php');
	require_once('core/database.php');
	require_once('core/safeParam.php');
	require_once('core/functions.php');
	require_once('core/xml.php');
	require_once('core/cartClass.php');
	require_once('core/pquery.php');
	require_once('core/pagination.php');
	
	$smarty->plugins_dir = array(SMARTY_DIR.'plugins/');
	
	// if you put &SMARTY_DEBUG in the url, a javascript window will pop-up with smarty debug info.
	$smarty->debugging_ctrl = 'URL';
	
	// setup array of directories for theme 'fallback' logic.
	$smarty->template_dir = array("$root/templates/".TEMPLATE, "$root/templates/basic");


	if(isset($_GET['site'])){
		unset($_SESSION['MYFAUCART']);
		unset($_SESSION['location']);
		unset($_SESSION['WCMP']);
	}

	$pquery= new PQuery();
	
	$smarty->assign('grid', $_SESSION['site']['grid']);
	
	//get sites.
	$sites = $database->select("SELECT * from config order by sitename");
	$smarty->assign('sites',$sites);

	/*GET OPTIONS CONFIG*/
	$config = getConfig();
	$smarty->assign('config',$config);
		
	
	
	/*DEFINE E-MAIL DEL DELIVERY*/
	if (!defined('ADMIN_EMAIL')){
  		define('ADMIN_EMAIL', $config['email']);
	}
	
		
	
	/*SET TIMEZONE*/
	date_default_timezone_set($config['timezone']);



	/*GET LANGUAJE*/
	require_once 'lang/'.$config['lang'].'/index.php';
	$smarty->assign('lang',$lang);



	/*GET HORARIOS*/
	$open = openTime($config['hours'],'open');
	$closed = openTime($config['hours'],'close');
	
	$blackout_start = openTime($config['hours'],'blackout_start');
	$blackout_end = openTime($config['hours'],'blackout_end');
	
	$date = date("F j, Y, g:i a");	
	$time = date("g:i a");

	$smarty->assign('open_times', $open);
	$smarty->assign('close_times', $closed);
	$smarty->assign('current_time',$time);
	$smarty->assign('current_date', $date);

	$dayofweek = date("l");
	$daysOfWeek = array("Sunday" => 0, "Monday" => 1, "Tuesday" => 2, "Wednesday" => 3, "Thursday" => 4, "Friday" => 5, "Saturday" => 6);
	$dayOfWeekNum = $daysOfWeek[$dayofweek];

	$open_time = $open[$dayOfWeekNum];	
	$close_time = $closed[$dayOfWeekNum];
	
	$blackout_start_time = $blackout_start[$dayOfWeekNum];
	$blackout_end_time =   $blackout_end[$dayOfWeekNum];
	
	if (!$open_time){
		$open_time = "12:00 am";
	}
	
	if (!$close_time){
		$close_time = "12:00 am";
	}

	$smarty->assign('dayweek', $dayOfWeekNum);
	$smarty->assign('open_time',$open_time);
	$smarty->assign('close_time',$close_time);
	$smarty->assign('bck_start', $blackout_start);
	$smarty->assign('bck_end', $blackout_end);

	$open_time_c = fixTime($open_time);
	$close_time_c = fixTime($close_time);

	if($close_time_c < "1200"){
		$close_time_c = $close_time_c + 2400;
	}

	$config['delivery_status'] = 1;

	/*Muestra si esta abierto o cerrado*/
	$thisTime = fixTime($time);
	
	if ( ($thisTime < $open_time_c or $thisTime > $close_time_c or $config['enabled'] == 1)
			or ( $thisTime > fixTime($blackout_start_time) && $thisTime < fixTime($blackout_end_time) )
			or ($open_time_c == "") ) {
		$smarty->assign('status',"closed");
		$smarty->assign('next_time_word',"open");
		$config['status'] = 0;
	} else {
		$smarty->assign('status',"open");
		$smarty->assign('next_time_word',"close");
		$config['status'] = 1;
  	}
?>
